package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("VideoController")
public class VideoController extends CmiEmaController {

	@RequestMapping("/listVideos")
	public ModelAndView listVideos(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = videoPaginationService.updatePagination(request, pagination);
		Set<Video> videos = videoPaginationService.getPaginationVideos(pagination);

		mav.addObject("videos", videos);
		mav.addObject("entityName", "video");
		mav.addObject("pagination", pagination);
		mav.setViewName("video/listVideos.jsp");
		return mav;
	}

	public String listVideos() {
		return "redirect:/listVideos";
	}

	@RequestMapping("/viewVideo")
	public ModelAndView viewVideo(@RequestParam Integer videoId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("video", videoDAO.findVideoByPrimaryKey(videoId));
		mav.setViewName("video/viewVideo.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteVideos")
	public ModelAndView confirmDeleteVideos(
			@RequestParam(value = "deleteVideosIds", required = true) Set<Integer> deleteVideosIds) {
		ModelAndView mav = new ModelAndView();

		Set<Video> deleteVideos = videoDAO.findVideosByIds(deleteVideosIds);
		Set<Video> referencedVideos = videoService.loadReferenceVideos(deleteVideos);
		boolean hasReferenced;
		if (referencedVideos.size() == 0) {
			mav.addObject("deleteEntitys", deleteVideos);
			hasReferenced = false;
		} else {
			deleteVideos.removeAll(referencedVideos);
			mav.addObject("referencedEntitys", referencedVideos);
			mav.addObject("deleteEntitys", deleteVideos);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Video");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listVideos");
		return mav;
	}

	@RequestMapping("/deleteVideo")
	public String deleteVideo(@RequestParam Integer videoId) {
		Video video = videoDAO.findVideoByPrimaryKey(videoId);
		videoService.deleteVideo(video);
		return "forward:/listVideos";
	}

	@RequestMapping("/confirmDeleteVideo")
	public ModelAndView confirmDeleteVideo(@RequestParam Integer videoId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("video", videoDAO.findVideoByPrimaryKey(videoId));

		mav.setViewName("video/deleteVideo.jsp");

		return mav;
	}

	@RequestMapping("/deleteVideos")
	public String deleteVideos(@RequestParam(value = "deleteVideosId", required = true) Set<Integer> deleteVideosId) {
		videoService.deleteVideos(deleteVideosId);
		return "forward:/listVideos";
	}

	@RequestMapping("/editVideo")
	public ModelAndView editVideo(@RequestParam Integer videoId) {

		ModelAndView mav = new ModelAndView();

		Video video = videoDAO.findVideoByPrimaryKey(videoId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("video", video);

		// 把所有的引用video对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(videoService.loadVideoObjectReferencedSet(video));

		// 将编辑页面video添加到mav中
		mav.setViewName("video/editVideo.jsp");
		return mav;
	}

	@RequestMapping("/newVideo")
	public ModelAndView newVideo() {
		ModelAndView mav = new ModelAndView();
		Video video = new Video();

		mav.addObject("video", video);

		// 把所有的引用video对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(videoService.loadVideoObjectReferencedSet(video));

		mav.addObject("newFlag", true);
		mav.setViewName("video/editVideo.jsp");

		return mav;
	}

	@RequestMapping("/saveVideo")
	public ModelAndView saveVideo(@ModelAttribute Video video) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = video.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Food foodTemp = video.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));

		Video existingVideo = CmiHibernateProxyUtil
				.deproxy(videoDAO.findVideoByPhysicalPrimaryKey(video.getName(), video.getPath()));

		video.setEvent(event);
		video.setFood(food);
		// 新建记录
		if (video.getId() == null) {
			if (video.equals(existingVideo)) {

				mav.addAllObjects(videoService.loadVideoClassReferencedSet(video));
				// mav = editContinue(mav, video);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("video/editVideo.jsp");
			} else {
				videoService.saveVideo(video);
				mav.setViewName("forward:/listVideos");
			}
		}
		// 编辑记录
		else {
			Video originVideo = CmiHibernateProxyUtil.deproxy(videoDAO.findVideoByPrimaryKey(video.getId()));
			if (!video.equals(originVideo) && video.equals(existingVideo)) {
				mav.addAllObjects(videoService.loadVideoClassReferencedSet(video));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("video/editVideo.jsp");
			} else {
				videoService.saveVideo(video);
				mav.setViewName("forward:/listVideos");
			}
		}
		return mav;
	}

	@RequestMapping("/videoController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
